 /*
        1.获得当前时间的时间对象
        2.获取当前时间的年月日
        3.拼接
        */
 const btn1 = document.getElementById('btn1')
 const btn2 = document.getElementById('btn2')
 const time12 = document.querySelector('.time12')
 const time24 = document.querySelector('.time24')

 function formatNow12() {
     const now = new Date()
     const year = now.getFullYear()
     const month = now.getMonth() + 1
     const day = now.getDate()
     let hour = now.getHours()
     const minute = now.getMinutes()
     const second = now.getSeconds()
     let t = ''
     if (hour < 12) {
         t = "上午"
     } else if (hour > 12) {
         t = "下午"
     } else {
         t = "中午"
     }
     hour = hour > 12 ? hour - 12 : hour

     const res = `${year}年${month}月${day}日 ${t} ${hour}:${minute}:${second}`
     return res
 }

 function formatNow24() {
     const now = new Date()
     const year = now.getFullYear()
     const month = now.getMonth() + 1
     const day = now.getDate()
     let hour = now.getHours()
     const minute = now.getMinutes()
     const second = now.getSeconds()

     const res = `${year}年${month}月${day}日 ${hour.toString().padStart(2,'0')}:${minute}:${second}`
     return res
 }

 btn1.onclick = function () {
     const time12Str = formatNow12()
     time12.innerText = time12Str

     const time24Str = formatNow24()
     time24.innerText = time24Str
 }
 /*
 1.获取输入的年月日
 2.生成一个事件对象，生成目标时间对象
 3.目标时间对象减去生成的时间对象
 4.计算天数
 */

 const yearIpt = document.getElementById('year')
 const monthIpt = document.getElementById('month')
 const dayIpt = document.getElementById('day')
 const dw = new Date(2020, 5, 25)

 function computeDays(year, month, day) {
     const date = new Date(year, month - 1, day)
     const milliseconds = dw - date //两个时间之间的毫秒数
     const days = milliseconds / 1000 / 60 / 60 / 24
     return Math.ceil(days)
 }
 const dwDaysEle = document.getElementById('dw-days')
 btn2.addEventListener('click', function () {
     const year = parseInt(yearIpt.value)
     const month = parseInt(monthIpt.value)
     const day = parseInt(dayIpt.value)
     if (isNaN(year) || isNaN(month) || isNaN(day)) {
         alert('你所输入的时间不合法！')
         return
     }
     const days = computeDays(year, month, day)
     dwDaysEle.innerText = days
 })

 /*
 1.生成一个now对象，获取年月日
 2.生成下班时间对象，根据现在的年月日生成一个当天下班时间对象
 3.两个时间相减
 4.运算
 */
 const cdHours = document.querySelector('.hour')
 const cdSecond = document.querySelector('.second')
 const cdMinute = document.querySelector('.min')

 function countDown() {
     const now = new Date()
     //获取当天的年月日
     const year = now.getFullYear()
     const monthIndex = now.getMonth()
     const day = now.getDate()
     //生成下班时间对象
     const goTime = new Date(year, monthIndex, day, 18)
     const milliseconds = goTime - now
     var hours = Math.floor(milliseconds / 1000 / 60 / 60 % 24);
     var minutes = Math.floor(milliseconds / 1000 / 60 % 60);
     var seconds = Math.floor(milliseconds / 1000 % 60);
     return [hours, minutes, seconds]
     // return `${hours}:${minutes}:${seconds}`

 }
 setInterval(function () {
     let timeArr = countDown()
     cdHours.innerText = timeArr[0]
     cdMinute.innerText = timeArr[1]
     cdSecond.innerText = timeArr[2]
 }, 1000)

 function getTime(){
    const now = new Date()
    const hour = now.getHours()
    const minute = now.getMinutes()
    const second = now.getSeconds()
    return [hour,minute,second]
 }

 const szHour = document.querySelector(".sz-hour")
 const szMinute = document.querySelector('.sz-min')
 const szSecond = document.querySelector('.sz-second')

 setInterval(function () {
    let timeArr = getTime()
    szHour.innerText = timeArr[0]
    szMinute.innerText = timeArr[1]
    szSecond.innerText = timeArr[2]
}, 1000)